<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<jsp:directive.page import="java.util.Date"/>

<jsp:directive.page import="Connectivity.*"/>
<jsp:directive.page import="Connectivity.ConnectionManager"/>
<jsp:directive.page import="java.sql.*"/>
<jsp:directive.page import="java.text.SimpleDateFormat"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'tmpService.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <%
  Date d1=new Date();
//servicedate=21-3-2012
//&call_type=PM
//&ataken=PRINTER+NO+POWER+ONZENITH+PC+OS+CURRUPT
//&mtaken1=mtaken
//&mgiven1=mgiven
//&pfp1=pfp
//&mtakendata=a
//&mgivendata=b
//&pfpdata=c
//&remark=d
//&ta=1&fe=2&lodge=3&other=&total=6
//&submit=submit
//&callentry_id=
//&complaint_id=1GI532012
//&emp_id=3034
//&call_lock_date=5-3-2012
//&call_lock_time=17%3A46%3A54
//&nature_fault=PRINTER
//&cust_id=179
  String servicedate = request.getParameter("servicedate")!=null?request.getParameter("servicedate"):"";
  String call_type = request.getParameter("call_type")!=null?request.getParameter("call_type"):"PM";
String strDate = servicedate;
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
//out.println("yyyy-mm-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("yyyy-mm-dd");
servicedate = formatter.format(date1);
//out.println("dd-MMM-yyyy date is ==>"+formattedDate);

  //String year=(servicedate.split("-",2).toString());
  //String month=(servicedate.split("-",1).toString());
 // String date=(servicedate.split("-",0).toString());
  //servicedate=year+"-"+month+"-"+date;
  String ataken = request.getParameter("ataken")!=null?request.getParameter("ataken"):"-";
  String mtakendata = request.getParameter("mtakendata")!=null?request.getParameter("mtakendata"):"-";
  String mgivendata = request.getParameter("mgivendata")!=null?request.getParameter("mgivendata"):"-";
  String pfpdata = request.getParameter("pfpdata")!=null?request.getParameter("pfpdata"):"-";
  String remark = request.getParameter("remark")!=null?request.getParameter("remark"):"-";
  String submit = request.getParameter("submit")!=null?request.getParameter("submit"):"";
  String callentry_id = request.getParameter("callentry_id")!=null?request.getParameter("callentry_id"):"";
  String complaint_id = request.getParameter("complaint_id")!=null?request.getParameter("complaint_id"):"";
  String call_lock_date = request.getParameter("call_lock_date")!=null?request.getParameter("call_lock_date"):"";
  String call_lock_time = request.getParameter("call_lock_time")!=null?request.getParameter("call_lock_time"):"";
  String nature_fault = request.getParameter("nature_fault")!=null?request.getParameter("nature_fault"):"";
  String cust_id = request.getParameter("cust_id")!=null?request.getParameter("cust_id"):"";
  String emp_eid = request.getParameter("emp_id")!=null?request.getParameter("emp_id"):"";
  
  
  
  String ta = request.getParameter("ta")!=null?request.getParameter("ta"):"0";
  String fe = request.getParameter("fe")!=null?request.getParameter("fe"):"0";
  String lodge = request.getParameter("lodge")!=null?request.getParameter("lodge"):"0";
  String other = request.getParameter("other")!=null?request.getParameter("other"):"0";
  String total = request.getParameter("total")!=null?request.getParameter("total"):"0";
  String call_closed_time=((d1.getHours())+":"+d1.getMinutes()+":"+d1.getSeconds());
  String createdby=""+(session.getAttribute("uname"));
  String firm_id=""+(session.getAttribute("firm_id"));
       
         if(ta.equals("")){ta="0";}
         if(fe.equals("")){fe="0";}
         if(lodge.equals("")){lodge="0";}
         if(other.equals("")){other="0";}
         if(total.equals("")){total="0";}
         
             if(remark.equals("")){remark="-";}
              if(pfpdata.equals("")){pfpdata="-";}
               if(mgivendata.equals("")){mgivendata="-";}
                if(mtakendata.equals("")){mtakendata="-";}
                 if(ataken.equals("")){ataken="-";}

out.println("servicedate:"+servicedate);
out.println("<br>call_type:"+call_type);
out.println("<br>ataken:"+ataken);
out.println("<br>mtakendata:"+mtakendata);
out.println("<br>mgivendata:"+mgivendata);
out.println("<br>pfpdata:"+pfpdata);
out.println("<br>remark:"+remark);
out.println("<br>fe:"+fe);
out.println("<br>ta:"+ta);
out.println("<br>lodge:"+lodge);
out.println("<br>other:"+other);
out.println("<br>total:"+total);
out.println("<br><br>call_closed_time:"+call_closed_time);
out.println("createdby:"+createdby);
out.println("firm_id:"+firm_id);
out.println("cust_id:"+cust_id);
//out.println("date:"+date);

     
     
try
{
 int rs1=0;
 Connection con=ConnectionManager.getConnection();
 String callstatus="CLOSED";
 if(pfpdata!=("-"))
 {  
   callstatus="PFP";
   
   
   }
  if(mtakendata!=("-"))
 {callstatus="MTAKEN";}
   if(mgivendata!=("-"))
 {callstatus="MGIVEN";}
///CallableStatement cstmt = con.prepareCall("{?=call fun_in_call_close(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
		//	cstmt.registerOutParameter(1, Types.INTEGER);
		//	cstmt.setString(2,servicedate);
    	//	cstmt.setString(3,call_type);
    	//	cstmt.setString(4,ataken);
    	//	cstmt.setString(5,mtakendata);
    	//	cstmt.setString(6,mgivendata);
    	//	cstmt.setString(7,pfpdata);
    	//	cstmt.setString(8,remark);
    	//	cstmt.setString(9,complaint_id);
	    ///	cstmt.setString(10,call_closed_time);
  		//	cstmt.setString(11,cust_id);
  		//	cstmt.setString(12,ta);
  		//	cstmt.setString(13,fe);
  		//	cstmt.setString(14,lodge);
  		//	cstmt.setString(15,other);
  		//	cstmt.setString(16,total);
		//	cstmt.setString(17,createdby);
		//	cstmt.setString(18,firm_id);
					
		//	con.setAutoCommit(false);
		//	cstmt.execute();
	//CallableStatement prst = con.prepareCall("{?=call fun_in_call_close(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
		//prst.registerOutParameter(1, Types.INTEGER);		
	//	prst.setString(2,complaint_id);
		//prst.setString(3,""+Integer.parseInt(cust_id));
		//prst.setString(4,""+Integer.parseInt(emp_id));
		//prst.setString(5,ataken);
		//prst.setString(6,call_type);
		//prst.setString(7,remark);
		//prst.setString(8,mtakendata);
		//prst.setString(9,mgivendata);
		//prst.setString(10,pfpdata);
		//prst.setString(11,callstatus);
		//prst.setString(12,servicedate);
		//prst.setString(13,createdby);
		//prst.setString(14,"INSERT");
		//prst.setString(15,"V");
		//prst.setString(16,""+Integer.parseInt(firm_id));
		//prst.setString(17,call_closed_time);
		//con.setAutoCommit(false);
		//boolean result=prst.execute(); 
		 
		 
		//	int result1 =(cstmt.getInt(1));
		//	out.println("result:"+result1);
			//if(result>0)
			//{response.sendRedirect("/Genius/form/general/CallAssign.jsp?status=I");}
String querry="insert into call_performed";
  querry=querry+"(complaint_id,cust_id,emp_id,action_taken,call_type,remark,";
  querry=querry+"mtaken,mgiven,pfp,call_status,issue_closed_date,";
  querry=querry+"  created_by,data_status,status,firm_id,call_closed_time)";
  querry=querry+"values(?,?,?,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd'),?,?,?,?,?)";
  
 PreparedStatement prst=con.prepareStatement(querry);
prst.setString(1,complaint_id);
	prst.setInt(2,Integer.parseInt(cust_id));
		prst.setInt(3,Integer.parseInt(emp_eid));
		prst.setString(4,ataken);
		prst.setString(5,call_type);
		prst.setString(6,remark);
		prst.setString(7,mtakendata);
		prst.setString(8,mgivendata);
		prst.setString(9,pfpdata);
		prst.setString(10,callstatus);
		prst.setString(11,servicedate);
		prst.setString(12,createdby);
		prst.setString(13,"INSERT");
		prst.setString(14,"V");
		prst.setInt(15,Integer.parseInt(firm_id));
		 prst.setString(16,call_closed_time);
		boolean result= prst.execute();
out.println("result:"+result);

if(result==false)
	{
	//String call_performed_id="(select(call_performed_id) from call_performed order by call_performed_id desc limit 1)"; 
												
		CallableStatement cs = null;
		 
		
		cs = con.prepareCall("{? = call fun_incall_performed(?,?,?)}");
		     cs.registerOutParameter(1,Types.INTEGER);
			 cs.setString(2,complaint_id);
 			 cs.setString(3,callstatus);
			 cs.setInt(4,Integer.parseInt(cust_id));
			cs.execute();
			
			int call_performed_id =(cs.getInt(1));
			out.println("call_performed_id:"+call_performed_id);
			if(call_performed_id>0)
			{
	
	
	 querry="INSERT INTO emp_expense(emp_id,cust_id,complaint_id,call_performed_id,issue_closed_date,ta,fe,lodge,other,total,status,data_status)";
       querry=querry+"values(?,?,?,?,to_date(?,'yyyy-mm-dd'),coalesce(?,0),coalesce(?,0),coalesce(?,0),coalesce(?,0),coalesce(?,0),?,?)";
  PreparedStatement prst1=con.prepareStatement(querry);
	 	prst1.setInt(1,Integer.parseInt(emp_eid));
		prst1.setInt(2,Integer.parseInt(cust_id));
		prst1.setString(3,complaint_id);
		prst1.setInt(4,call_performed_id);
	    prst1.setString(5,servicedate);
		prst1.setInt(6,Integer.parseInt(ta));
		prst1.setInt(7,Integer.parseInt(fe));
		prst1.setInt(8,Integer.parseInt(lodge));
		prst1.setInt(9,Integer.parseInt(other));
		prst1.setInt(10,Integer.parseInt(total));
		prst1.setString(11,"V");
		prst1.setString(12,"INSERT");
		boolean result1= prst1.execute();
	if(result1==false)
	response.sendRedirect("/Genius/form/general/CallPerformed1.jsp?status=I&call_performed_id="+call_performed_id); //logged-in page}
	}


}



}
catch(Exception e)
{}  
   %>
 </body>
</html>
